import 'package:flutter/material.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.dart';

class MapWidget extends StatefulWidget {
  final double height;
  final Function(BMFMapController)? onMapCreated;

  const MapWidget({super.key, required this.height, this.onMapCreated});

  @override
  State<MapWidget> createState() => _MapWidgetState();
}

class _MapWidgetState extends State<MapWidget> {
  BMFMapController? _mapController;
  final LocationFlutterPlugin _locPlugin = LocationFlutterPlugin();
  BMFUserLocation? _userLocation;

  @override
  void initState() {
    super.initState();
    _setupLocationCallbacks();
  }

  @override
  void dispose() {
    _locPlugin.stopLocation();
    super.dispose();
  }

  void _setupLocationCallbacks() {
    _locPlugin.seriesLocationCallback(callback: _handleLocationUpdate);
    _locPlugin.updateHeadingCallback(callback: _handleHeadingUpdate);
  }

  void _handleLocationUpdate(BaiduLocation result) {
    if (result.latitude == null || result.longitude == null) return;

    final coordinate = BMFCoordinate(result.latitude!, result.longitude!);
    final location = BMFLocation(
      coordinate: coordinate,
      altitude: result.altitude,
      course: result.course,
      accuracy: result.radius,
    );

    setState(() {
      _userLocation = BMFUserLocation(location: location);
    });

    _mapController?.updateLocationData(_userLocation!);
    _mapController?.setCenterCoordinate(coordinate, true);
  }

  void _handleHeadingUpdate(BaiduHeading heading) {
    if (_userLocation?.location == null) return;

    final userLocation = BMFUserLocation(
      location: _userLocation!.location,
      heading: BMFHeading.fromMap(heading.getMap()),
    );

    _mapController?.updateLocationData(userLocation);
  }

  BMFMapOptions _initMapOptions() {
    return BMFMapOptions(
      center: BMFCoordinate(40.055101313423215, 116.43356500172574),
      zoomLevel: 15,
      mapType: BMFMapType.Standard,
    );
  }

  void _onMapCreated(BMFMapController controller) {
    _mapController = controller;
    _updateUserLocationDisplayParam();
    _locPlugin.startLocation();

    if (widget.onMapCreated != null) {
      widget.onMapCreated!(controller);
    }
  }

  void _updateUserLocationDisplayParam() {
    if (_mapController == null) return;

    final displayParam = BMFUserLocationDisplayParam(
      locationViewImageNew: 'assets/images/user.png',
      isAccuracyCircleShow: true,
      accuracyCircleFillColor: Colors.blue,
      locationViewHierarchy:
          BMFLocationViewHierarchy.LOCATION_VIEW_HIERARCHY_BOTTOM,
    );

    _mapController!.updateLocationViewWithParam(displayParam);
    _mapController!.showUserLocation(true);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: widget.height,
      child: BMFMapWidget(
        onBMFMapCreated: _onMapCreated,
        mapOptions: _initMapOptions(),
      ),
    );
  }
}
